#include "comm.hpp"
#include "Fifo.hpp"

int main()
{
	// 1、创建和获取共享空间唯一值key
	key_t key = GetShmKeyOrDie();
	cout << "shm_key:" << ToHex(key) << endl;

	// 2、获取共享内存
	int shmid = GetShm(key, defaultsize);
	cout << "shmid : " << shmid << endl;

	// 3、挂载关联
	char *addr = (char *)ShmAttach(shmid);
	cout << "client ShmAttach addr: " << ToHex((uint64_t)addr) << endl;
	// ShmDubge(shmid);

	//引入管道，实现进程协同
	memset(addr, 0, defaultsize);
	Sysn syn;
	syn.OpenWriteOrDie();

	// 通信
	// 共享内存的方式有一个缺点就是没有进程协同，不会一起终止，可以利用管道来解决
	cout << "client stay say..." << endl;
	for (char c = 'A'; c <= 'Z'; c++)
	{
		addr[c - 'A'] = c;
		sleep(1);
		syn.WakeUp();
	}

	// 4、取消关联
	ShmDetach(addr);
	cout << "ShmDetach success,addr: " << ToHex((uint64_t)addr) << endl;

	return 0;
}